<?php

namespace app\common\model;


use think\Model;

class Answer extends Model
{
   protected $name='answer';

    /***
     * @return \think\model\relation\BelongsTo
     * 关联问题表
     */
   public function ask()
   {
       return $this->belongsTo('Ask','ask_id')->field('id,user_id,scenic_id,content,create_time');
   }

    /***
     * 回答列表
     */
   public function answerList($where = [],$page)
   {
        $list = $this->with(['ask'])
                     ->where($where)
                     ->order('create_time desc')
                     ->page($page, 10)
                     ->select();

        $list = collection($list)->toArray();

        foreach ($list as &$value){
            $value['create_time'] = date('Y-m-d H:i:m', $value['create_time']);
            $value['ask']['create_time'] = date('Y-m-d H:i:m', $value['ask']['create_time']);
            $value['answer_count'] = $this->where(['ask_id'=>$value['ask_id']])->count();
            $value['answer_count'] =  $value['answer_count'] - 1;    //减去自己的答案
        }
        return $list;
   }
}